package com.easy.web.rx;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * @Packge com.easy.web.rx
 * @Autor naibin.lin
 * @Description 响应式编程 demo
 * @Version 1.0
 * @Date 2022-07-29 16:22
 */
public class RxDemo {


    private static class S {

    }


    /**
     * 0 - 1 个元素
     */
    public static void mono() {
        Mono<String> noData = Mono.empty();
    }


    /**
     * 0 - n 个元素
     */
    public static void flux() throws InterruptedException {
        // 创建Flux或Mono，并订阅它们
        Flux<Executor> seq = Flux.just(new ExecutorSuccess(), new ExecutorError());
        //  seq.subscribe(e -> {
        //
        //    });

        //  seq.subscribe(System.out::println);

        Supplier map = HashMap::new;

        seq.map(exe -> {
                    exe.doSome();
                    return exe;
                }).take(2)
                .subscribe(
                        RxDemo::sucMsg,
                        RxDemo::errorMsg,
                        () -> {
                            // 结束了 调用我
                            System.out.println("complete");
                        }
                );


/*        seq.next();
        seq.next();
        seq.next();
        seq.next();*/


        System.out.println(seq);


        // 防止 主线程过早结束
        Thread.sleep(2000l);

    }


    public static void sucMsg(Object o) {
        Executor executor = (Executor) o;
        System.out.println("=========================成功了执行我==========================" + executor);
        // executor.doSome();
        //  System.out.println("成功。。");

        //  executor.doSome();
    }

    public static void errorMsg(Throwable throwable) {
        System.err.println("=========================错误le执行我==========================" + throwable.getMessage());
    }


    public static void main(String[] args) throws Exception {
        flux();
    }
}
